
import 'package:baby/UI/Widgets/HtmlContent/BuiltInBindings/TagAudio.dart';
import 'package:baby/UI/Widgets/HtmlContent/BuiltInBindings/TagBr.dart';
import 'package:baby/UI/Widgets/HtmlContent/BuiltInBindings/TagDiv.dart';
import 'package:baby/UI/Widgets/HtmlContent/BuiltInBindings/TagH.dart';
import 'package:baby/UI/Widgets/HtmlContent/BuiltInBindings/TagImg.dart';
import 'package:baby/UI/Widgets/HtmlContent/BuiltInBindings/TagP.dart';
import 'package:baby/UI/Widgets/HtmlContent/BuiltInBindings/TextInDom.dart';
import 'package:baby/UI/Widgets/HtmlContent/TransformPipeline.dart';
import 'package:flutter/material.dart';
import 'package:html/dom.dart' as prefix0;
import 'package:html/parser.dart'as Parser;

class HtmlContent extends StatefulWidget {
  HtmlContent({Key key, this.staticHtml, this.pipes, this.baseUrl, this.activeColor, this.inactiveColor}) : super(key: key);
	TransformPipeLine<HtmlContentState, Object> pipes;

	final String staticHtml;
	Uri baseUrl;
	Color activeColor;
	Color inactiveColor;

  @override createState() => HtmlContentState();
}

class HtmlContentState extends State<HtmlContent> {
	List<prefix0.Node> elements;

	@override
  void initState() {
		elements = Parser.parse(widget.staticHtml,).body.nodes;
    super.initState();
  }

  void _init(){
		widget.activeColor ??= Theme.of(context).primaryColor;
		widget.inactiveColor ??= Theme.of(context).primaryColor;

		if (widget.pipes == null) {
			widget.pipes = TransformPipeLine<HtmlContentState, Object>(featureContext: this);
			var pipes = widget.pipes;

			pipes.installPipe(TagP());
			pipes.installPipe(TagImg());
			pipes.installPipe(TagH());
			pipes.installPipe(TagDiv());
			pipes.installPipe(TextInDom());
			pipes.installPipe(TagBr());
			pipes.installPipe(TagAudio());
		}
	}

  @override
  Widget build(BuildContext context) {

		_init();

  	var widgets = _buildFromElements(elements);
    return Column(
			mainAxisSize: MainAxisSize.min,
			crossAxisAlignment: CrossAxisAlignment.start,
			children: widgets,
		);
  }

  List<Widget> _buildFromElements(List<prefix0.Node> elements){
  	List<Widget> widgets = [];

		elements.forEach((item){
			var result = widget.pipes.start(item);
			if (result is Widget) widgets.add(result);
		});

  	return widgets;
	}
}

